Ext.define('AmpleHarvest.objects.paging.cPages',
{
	extend			: 'Ext.grid.Panel',
	alias			: 'widget.ampleharvest-objects-paging-cpages',
	title			: 'Pages',
	iconCls			: 'toolbar-article-navigation-pages',
	collapsible		: true,
	m_str_CurID		: '',
	columns			:
	[
			{
				header		: 'Name',
				dataIndex	: 'name',
				flex		: 1
			}
	],
//	store			: Ext.create('Ext.data.Store',
//			{
//				fields	:
//				[
//					'name'
//				],
//				data	: {},
//				proxy	:
//				{
//					type	: 'memory',
//					reader	:
//					{
//						type	: 'json',
//						root	: 'items'
//					}
//				}
//			}),
	listeners		:
	{
		'itemclick'	:
		{
			fn	: function(obj_Record, obj_Item, obj_Row, int_E)
			{
				var obj_server = obj_Item.get('server');
				var obj_id = obj_Item.get('id');
				// alert(obj_server+'::'+obj_id);
				m_obj_AmpleHarvest_ViewPort
						.fireEvent(
								AmpleHarvest.eventHandler.cWindowEvents.C_STR_EVENT_REDIRECT,
								obj_server, obj_id
						);

			}
		}
	},
	simpleSelect	: true,

	viewConfig		:
	{
		getRowClass	: function(obj_Record)
		{
			return 'clickable';
		}
	},

	initComponent	: function()
	{
		var obj_This = this;

		this.store = Ext.create('Ext.data.Store',
				{
			fields	:
			[
				'name'
			],
			data	: {},
			proxy	:
			{
				type	: 'memory',
				reader	:
				{
					type	: 'json',
					root	: 'items'
				}
			}
		});
		
		this.callParent();
	},

	setCurID		: function(str_ID)
	{
		this.m_str_CurID = str_ID;
	},

	add_JsonObject	: function(obj_Json)
	{
		this.itemCount = 0;
		var bool_Run = false;
		var arr_Pages = [];
		for (var i in obj_Json)
		{
			arr_Pages[obj_Json[i].pageNum] = obj_Json[i];
		}
		for (var i in arr_Pages)
		{
			i = parseInt(i);
			if (arr_Pages[i].pageNum)
			{
				bool_Run = true;
				var str_Name = "Page " + arr_Pages[i].pageNum;
				if (arr_Pages[i].id == this.m_str_CurID)
				{
					str_Name = str_Name + " - Current";

					if (arr_Pages[i].pageNum < arr_Pages.length)
					{
						if (arr_Pages[i + 1])
						{
							m_obj_AmpleHarvest_ViewPort    /// TODO: ESCALATE THIS TO THE MAIN WINDOWEVENT - non view specific
									.fireEvent(
											AmpleHarvest.views.article.eventHandler.cWindowEvents.C_STR_EVENT_ARTICLE_PAGE_NEXT_ACTIVATE,
											arr_Pages[i + 1].serverId,
											arr_Pages[i + 1].id
									);
						}
					}

					if (arr_Pages[i].pageNum > 1)
					{
						if (arr_Pages[i - 1])
						{
							m_obj_AmpleHarvest_ViewPort/// TODO: ESCALATE THIS TO THE MAIN WINDOWEVENT - non view specific
									.fireEvent(
											AmpleHarvest.views.article.eventHandler.cWindowEvents.C_STR_EVENT_ARTICLE_PAGE_PREVIOUS_ACTIVATE,
											arr_Pages[i - 1].serverId,
											arr_Pages[i - 1].id
									);
						}
					}
				}
				this.store.add(AmpleHarvest.DATA.Model.cMetadata.create(
						{
							name	: str_Name,
							server	: arr_Pages[i].serverId,
							id		: arr_Pages[i].id
						}));
				this.itemCount++;
			}
		}

		return bool_Run;
	},

	hasData			: function()
	{
		if (!this.itemCount)
		{
			return false;
		}
		if (this.itemCount < 1)
		{
			return false;
		}
		this.setTitle(this.title + ' (' + this.itemCount + ')');
		return true;
	}
}
);
